using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace DynamicTextTest
{
    public partial class StartForm : Form
    {
        private SharpMud.Text.Dynamic.SimpleDynamicTextBody sDTB = new SharpMud.Text.Dynamic.SimpleDynamicTextBody();
        private bool IsDirty;


        public StartForm()
        {
            InitializeComponent();
            newToolStripMenuItem_Click(null, null);
        }

        private void btnGenerateCode_Click(object sender, EventArgs e)
        {
            SharpMud.Text.Dynamic.Processing.IProcessedDynamicTextBody pDTB = SharpMud.Text.Dynamic.Processing.DynamicTextProcessor.Process(sDTB, null);
            System.CodeDom.Compiler.CodeDomProvider provider = pDTB.CodeDomProvider;

            System.IO.StringWriter sw = new System.IO.StringWriter();
            System.CodeDom.CodeCompileUnit ccu = pDTB.ToCodeCompileUnit().CodeObject;
            System.CodeDom.Compiler.CodeGeneratorOptions cgo = new System.CodeDom.Compiler.CodeGeneratorOptions();
            cgo.VerbatimOrder = true;
            provider.GenerateCodeFromCompileUnit(ccu, sw, cgo);
            sw.Flush();
            (new CodeForm(sw.ToString(), provider, pDTB)).ShowDialog();
        }

        #region Menu Strip Items
        private void newToolStripMenuItem_Click(object sender, EventArgs e)
        {
            sDTB = new SharpMud.Text.Dynamic.SimpleDynamicTextBody();
            sDTB.Contents = StartForm.DefaultContents;
            sDTB.FileName = "";
            textBox1.Text = sDTB.Contents;

            IsDirty = true;
        }

        private void openToolStripMenuItem_Click(object sender, EventArgs e)
        {
            DoOpen();
        }

        private void saveToolStripMenuItem_Click(object sender, EventArgs e)
        {
            DoSave();
        }

        private void saveAsToolStripMenuItem_Click(object sender, EventArgs e)
        {
            DoSaveAs();
        }

        private void exitToolStripMenuItem_Click(object sender, EventArgs e)
        {
            DoExit();
        }
        #endregion

        bool DoSave()
        {
            if (sDTB.FileName == String.Empty || sDTB.FileName == null)
            {
                return DoSaveAs();
            }
            else
            {
                System.IO.StreamWriter sw = new System.IO.StreamWriter(sDTB.FileName, false);
                sw.Write(sDTB.Contents);
                sw.Close();

                IsDirty = false;
                
                return true;
            }
        }

        bool DoSaveAs()
        {
            saveFileDialog1.Reset();

            saveFileDialog1.RestoreDirectory = true;
            saveFileDialog1.Filter = "Dynamic Text Files (*.txtx)|*.txtx";

            DialogResult dr = saveFileDialog1.ShowDialog();
            if (dr == DialogResult.Cancel)
                return false;

            sDTB.FileName = saveFileDialog1.FileName;

            return DoSave();
        }

        bool DoOpen()
        {
            openFileDialog1.Reset();
            
            openFileDialog1.RestoreDirectory = true;
            openFileDialog1.Filter = "Dynamic Text Files (*.txtx)|*.txtx";
            openFileDialog1.CheckFileExists = true;
            openFileDialog1.CheckPathExists = true;
            
            DialogResult dr = openFileDialog1.ShowDialog();
            if (dr == DialogResult.Cancel)
                return false;
            string fileName = openFileDialog1.FileName;

            System.IO.FileInfo fi = new System.IO.FileInfo(fileName);
            if (!fi.Exists)
            {
                MessageBox.Show("File does not exist!");
                return false;
            }

            System.IO.StreamReader sr = fi.OpenText();
            string fileContents = sr.ReadToEnd();
            sr.Close();

            sDTB = new SharpMud.Text.Dynamic.SimpleDynamicTextBody();
            sDTB.FileName = fileName;
            sDTB.Contents = fileContents;
            textBox1.Text = fileContents;
            IsDirty = false;

            return true;
        }

        bool DoExit()
        {
            if (CheckSave())
            {
                Application.Exit();
                return true;
            }
            else
            {
                return false;
            }
        }

        bool CheckSave()
        {
            if (IsDirty)
            {
                DialogResult dr = MessageBox.Show("Would you like to save your changes first?", this.Text, MessageBoxButtons.YesNoCancel);
                if (dr == DialogResult.Cancel)
                {
                    return false;
                }
                else if (dr == DialogResult.Yes)
                {
                    return DoSave();
                }
                else if (dr == DialogResult.No)
                {
                    return true;
                }
            }
            else
            {
                return true;
            }
            return true;
        }

        private static string DefaultContents
        {
            get
            {
                System.Text.StringBuilder sb = new StringBuilder();
                sb.Append(@"<%@ Page ClassName=""Test"" Language=""Microsoft.VisualBasic.VBCodeProvider, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"" %>" + System.Environment.NewLine);
                sb.Append(@"<%@ Import Namespace=""System"" %>" + System.Environment.NewLine);
                sb.Append(@"Howdy!" + System.Environment.NewLine);
                sb.Append(@"The time is <%=System.DateTime.Now.ToString()%>!" + System.Environment.NewLine);
                sb.Append(@"" + System.Environment.NewLine);
                sb.Append(@"This is Fibonacci:" + System.Environment.NewLine);
                sb.Append(@"==============" + System.Environment.NewLine);
                sb.Append(@"" + System.Environment.NewLine);
                sb.Append(@"<%" + System.Environment.NewLine);
                sb.Append(@"Dim X1 as Long = 0" + System.Environment.NewLine);
                sb.Append(@"Dim X2 as Long = 1" + System.Environment.NewLine);
                sb.Append(@"Dim X3 as Long = X1 + X2" + System.Environment.NewLine);
                sb.Append(@"Dim I as Integer = 0" + System.Environment.NewLine);
                sb.Append(@"" + System.Environment.NewLine);
                sb.Append(@"For I = 1 To 85 Step 1" + System.Environment.NewLine);
                sb.Append(@"X1 = X2" + System.Environment.NewLine);
                sb.Append(@"X2 = X3" + System.Environment.NewLine);
                sb.Append(@"X3 = X1 + X2" + System.Environment.NewLine);
                sb.Append(@"%><%=X3.ToString()%>" + System.Environment.NewLine);
                sb.Append(@"<%" + System.Environment.NewLine);
                sb.Append(@"Next" + System.Environment.NewLine);
                sb.Append(@"%>" + System.Environment.NewLine);
                return sb.ToString();
            }
        }

        private void textBox1_TextChanged(object sender, EventArgs e)
        {
            sDTB.Contents = textBox1.Text;
        }
    }
}